<?php

namespace app\modules\controllers;
use app\models\Parts;
use Yii;
use yii\web\Controller;
use app\models\Follow;
use app\models\YiiUser;
use yii\data\Pagination;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;

class partsController extends Controller{
    public $layout  = 'layout';

	    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{parts}}';
    }
    /**
     * @用户授权规则
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['sendmsg','mds','msg','read','mysend','reply','pull'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }


 
    /**
     * @return string 后台默认页面
     */
    public function  actionMds(){

        $model=new parts();
	    $uid=Yii::$app->user->getId();
        $count=$model->find()->count();
        $page=new Pagination(['defaultPageSize'=>10,'totalCount'=>$count]);
        $df=$model->find()->offset($page->offset)->limit($page->limit)->all();
        return $this->render('mds',['page'=>$page,'parts'=>$df]);


    }
 

 /**
     * @reply 在当前消息下回复对方
     */

    public function  actionReply(){
        $arr=array();
        if(Yii::$app->request->isAjax){
            $msg=Yii::$app->request->post("Msg");
            $obj=new Msg();
            $obj->fid=$msg['fid'];
            $obj->tid=$msg['tid'];
            $obj->title=$msg['title'];
            $obj->content=$msg['content'];
            $obj->reply=$msg['reply'];
            $obj->send_time=time();
            $obj->save();
            $arr['status']=1;

        }else{
            $arr['status']=0;
        }
        echo json_encode($arr);
    }

    /**
     * @pull  前台轮询消息推送
     */

    public function actionPull(){
        $arr=array();
        if(Yii::$app->request->isAjax){
            $num=Yii::$app->request->get('msgnum');
            //对比数据库当前用户未读消息结果
            $msgnum=Msg::find()->where(['tid'=>Yii::$app->user->getId()])->andWhere(['status'=>0])->count();
            if($msgnum!=$num){
                $session=Yii::$app->session;
                $session->set('msg',$msgnum);
                $arr['msgnum']=$msgnum;
                $arr['status']=1;
            }else{
                $arr['status']=0;
            }
            echo json_encode($arr);
        }
    }


    /**
     * @read read mesage
     */
    public function actionRead($id){
        $msg=Msg::findOne(['id'=>$id]);

        //设置为已读
        if($msg->status==0){
            $msg->status=1;
            $msg->save();
        }

        //消息总数减1
        $session=Yii::$app->session;
        $num=$session->get('msg');
        if($num>0){
            $num--;
            $session->set('msg',$num);
        }

        //回复表单模型
        $model=new Msg();

        return $this->render('read',['msg'=>$msg,'model'=>$model]);
    }





}
